#include <REGX52.H>
#include"key.h"
#include"Nixie.h"
#include"Timer0.h"
#include"AT24C02.h"
#include"Delay.h"
#include"I2C.h"

unsigned char keyn;
unsigned char Min,Sec,MinSec;
unsigned char RunFlag;
void main()
{
	Timer0_Init();
 	while(1)
	{
		
		if(keyn)
		{
			keyn=key_end();
		if(keyn==1)			
		{
			RunFlag=!RunFlag;	
		}
		if(keyn==2)			
		{
			Min=0;				
			Sec=0;
			MinSec=0;
		}
		if(keyn==3)			
		{
			WRITE_BITY(0,Min);	
			Delay(5);
			WRITE_BITY(1,Sec);
			Delay(5);
			WRITE_BITY(2,MinSec);
			Delay(5);
		}
		if(keyn==4)			
		{
			Min=RCE_BITY(0);	
			Sec=RCE_BITY(1);
			MinSec=RCE_BITY(2);
		}
			Nixie_set(1,Min/10);
			Nixie_set(2,Min%10);
			Nixie_set(3,11);
			Nixie_set(4,Sec/10);
			Nixie_set(5,Sec&10);
			Nixie_set(6,11);
			Nixie_set(7,MinSec/10);
			Nixie_set(8,MinSec%10);
		}
	}
}
void sec_loop(void)
{
	if(RunFlag)
	{
		MinSec++;
		if(MinSec>=100)
		{
			MinSec=0;
			Sec++;
			if(Sec>=60)
			{
				Sec=0;
				Min++;
				if(Min>=60)
				{
					Min=0;
				}
			}
		}
	}
}
void Timer0_Routine() interrupt 1
{
	static unsigned int T0Count1,T0Count2,T0Count3;
	TL0=0x18;
	TH0=0xFC;
	T0Count1++;
	
	if(T0Count1>=20)
	{
		
		T0Count1=0;
		key_loop();
	}
	T0Count2++;
	if(T0Count2>=2)
	{
		
		T0Count2=0;
		Nixie_loop();
	}
	T0Count3++;
	if(T0Count3>=10)
	{
		T0Count3=0;
		sec_loop();
	}
}